#!/bin/bash

# 设置源文件夹和目标文件夹的路径
source_folder="/home/jenkins/workspace"
target_folder="/home/code"

cd $source_folder
# 遍历源文件夹中的所有文件夹
for folder in $(ls -d $source_folder/*/)
do
    # 获取文件夹名称
    folder_name=$(basename "$folder")
    echo "folder name $folder_name"

    # 判断文件夹是否以tmp结尾、或者是以docker开头
    if [[ $folder_name == *@tmp || $folder_name == docker*]]; then
        # 如果是以tmp结尾、或者是以docker开头，则跳过当前文件夹
        continue
    else
        # 如果不是以@tmp结尾，则判断是否以backend结尾
        if [[ $folder_name == *backend ]]; then
            # 如果以backend结尾，则进入文件夹，执行mvn clean，然后返回上级目录
            cd "$folder"
            /opt/apache-maven-3.6.3/bin/mvn clean
            cd ..
            # 拷贝文件夹及其子文件到目标文件夹
            cp -r "$folder" "$target_folder"
        fi

        # 如果不是以@tmp结尾，则判断是否以frontend结尾
        if [[ $folder_name == *frontend ]]; then
            rsync -av --exclude='node_modules' "$folder_name" "$target_folder"
        fi
    fi
done